<template>
  <div>
    <div class="permissManaTool">
      <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input size="small" placeholder="请输入角色中文名" v-model="role.nameZh"/>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addRole" @keydown.enter.native="addRole">添加角色</el-button>
    </div>
    <div class="primissManaMain">
      <el-collapse accordion @change="change" v-model="activeName">
        <el-collapse-item :title="role.nameZh" :name="role.id" v-for="(role,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button style="float: right; padding: 3px 0; color: red" type="text" icon="el-icon-delete" @click="deleteRole(role)"></el-button>
            </div>
            <div>
              <el-tree :data="allMenus" :props="defaultProps"
                       show-checkbox :default-checked-keys="selectedMenus"
                       ref="tree" :key="index"
                       node-key="id"></el-tree>
              <div style="display: flex; justify-content: flex-end">
                <el-button size="mini" type="primary" @click="doUpdate(role.id,index)">确认修改</el-button>
                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data(){
    return{
      role:{
        name:'',
        nameZh:''
      },
      roles:[],
      allMenus:[],
      defaultProps: {
        // 匹配子属性名（哪个属性是子属性）
        children: 'children',
        // 匹配显示出来的名字(哪个属性要被显示)
        label: 'name'
      },
      selectedMenus: [],
      activeName:-1
    }
  },
  methods:{
    initRoles(){
      this.getRequest('/sys/basic/permiss/').then(resp =>{
        if (resp){
          this.roles = resp;
        }
      })
    },
    initMenus(){
      this.getRequest('/sys/basic/permiss/menus').then(resp =>{
        if (resp){
          this.allMenus = resp;
        }
      })
    },
    initSelectedMenus(id){
      this.getRequest('/sys/basic/permiss/mid/'+id).then(resp =>{
        if (resp){
          this.selectedMenus = resp;
        }
      })
    },
    change(rid){
      // 如果存在rid
      // 因为这个函数在item被点击的时候触发，item展开和折叠都会被触发，所以我们需要判断一下是展开还是折叠
      if (rid){
        this.initSelectedMenus(rid);
        this.initMenus();
      }
    },
    doUpdate(rid,index){
      let selectKeys = this.$refs.tree[index].getCheckedKeys();
      let url = '/sys/basic/permiss/?rid='+rid;
      selectKeys.forEach(key =>{
        url+='&mids='+key;
      })
      this.putRequest(url).then(resp =>{
        if (resp){
          this.initRoles();
          this.activeName = -1;
        }
      })
    },
    cancelUpdate(){
      this.activeName = -1;
    },
    addRole(){
      if (this.role.name && this.role.nameZh){
        this.postRequest('/sys/basic/permiss/role',this.role).then(resp =>{
          if (resp){
            this.role.name = '';
            this.role.nameZh = '';
            this.initRoles();
          }
        })
      }else {
        this.$message.warning("请先输入内容");
      }
    },
    deleteRole(role){
      this.$confirm('此操作将永久删除['+role.nameZh+']角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/sys/basic/permiss/'+role.id).then(resp =>{
          if (resp){
            this.initRoles();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  created() {
    this.initRoles();
  }
}
</script>

<style scoped>

.permissManaTool{
  display: flex;
  justify-content: flex-start;
}

.permissManaTool .el-input{
  width: 300px;
  margin-right: 8px;
}

.primissManaMain{
  margin-top: 10px;
  width: 700px;
}

</style>